package de.fhb.itunes.controller.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import de.fhb.itunes.manager.filewatcher.OrdnerUebweachung;
import de.fhb.itunes.manager.mail.Mail;
import de.fhb.itunes.manager.mail.Template.HTML_Templates;
import de.fhb.itunes.manager.mail.Template.Template;
import de.fhb.itunes.manager.mail.Template.TemplateBuilder;

/**
 * Servlet implementation class ReadMailContent
 */
@WebServlet("/ReadMailContent")
public class ReadMailContent extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ReadMailContent() {
        super();
       
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if (session != null){
		OrdnerUebweachung ordner = new OrdnerUebweachung();
		ordner.checkDir();
		
		ArrayList<String> test = ordner.getFilmeNames();//new ArrayList<String>();
		
//		test.add("titel1");
//		test.add("titel2");
		//session.setAttribute("List_filme", ordner.getFilmeNames());
		session.setAttribute("List_filme", test);
		request.setAttribute("List_filme", test);
		TemplateBuilder tb = new TemplateBuilder(ordner.getFilmeNames());
		session.setAttribute("tb", tb);
		
		}
		
		
		
		String nextJSP = "/ReadInforamtionForEmail.jsp";
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
		dispatcher.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String beschreibung = "";
        String bildurl ="";
        HttpSession session = request.getSession();
		if (session.isNew()){
		System.out.println("Fehler");
		} else{
			TemplateBuilder tb = (TemplateBuilder) session.getAttribute("tb");
			
//aus dem request alle parameterauslesen
			response.setContentType("text/html;charset=UTF-8");
	        ArrayList<Template> temps= new ArrayList<Template>();
	        Enumeration en = request.getParameterNames();
	        int count = 0;
	        while (en.hasMoreElements()) {
	            count ++;
	            String paramName = (String) en.nextElement();
	            String paraValue = request.getParameter(paramName);
	            if (paramName.contains("beschreibung")){
	            	beschreibung = paraValue;
	            } else if(paramName.contains("bild_url")){
	            	bildurl = paraValue;
	            }
	            if (count != 0 && count % 2 == 0){
	            	if(beschreibung != "")
	            	temps.add(new Template(HTML_Templates.textWithLeftAlignedImage, "test", beschreibung, bildurl));
	            }
	            
	        }
			
			Template standard = new Template(HTML_Templates.textonly,"Es gibt gute Nachrichten!!! Es wurden neue Titel zu Ihrer Mediathek hinzugefügt","","");
			
//			Template one = new Template(HTML_Templates.fullwidthImage,"Das ist ein Text","Das ist der Titel","http://www.google.de/intl/de_ALL/images/logos/images_logo_lg.gif");
//			Template two = new Template(HTML_Templates.textWithLeftAlignedImage,"Das ist ein Text","Das ist der Titel","http://www.google.de/intl/de_ALL/images/logos/images_logo_lg.gif");
			
			Mail mail = new Mail();
			mail.addTemplate(standard);
			for (Template template : temps) {
				mail.addTemplate(template);
			}
			mail.setSubject("Nachricht von iTunes Media");
			mail.send();
			String nextJSP = "/prepareMail.jsp";
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
			dispatcher.forward(request,response);
		}
        
	}

}
